<?php

/**
 * HTTP Class
 * @author ReGeDa
 */
class HTTP extends Plugin_Object {
    
	/**
	 * Get persistant plugin state
	 * @override
	 * @return boolean
	 */
	public function is_persistant() {
		return TRUE;
	}

	/**
	 * Set HTTP header
	 * @param string $var
	 * @param string $value
	 */
	public function set_header($var, $value = '') {
		if (is_array($var)) {
			foreach($var as $key => $val)
				header($key.': '.$val);
		}
		else
			header($var.': '.$value);
	}

	/**
	 * Set response code
	 * @param integer $http_code
	 */
	public function set_response_code($http_code) {
		header('HTTP/1.1 '.$http_code.' '.$this->header_message($http_code), TRUE, $http_code);
	}

	/**
	 * Get header message
	 * @param integer $http_code
	 * @return string
	 */
	public function header_message($http_code) {
		return constant('HTTP_'.$http_code);
	}

	/**
	 * Append protocol schema and base URL
	 * @param string $url
	 * @return string
	 */
	public function site_url($url) {
		return ! preg_match('#^https?://#i', $url) ? config('base_url').$url : $url;
	}

	/**
	 * Redirect to URL
	 * @param string $url
	 * @param integer $http_response_code
	 */
	public function redirect($url = '', $http_response_code = 302) {
		header('Location: '.$this->site_url($url), TRUE, $http_response_code);
		platcode()->terminate();
	}
}

// end of file